package com.spzx.user.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.security.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.spzx.user.mapper.UserInfoMapper;
import com.spzx.user.api.domain.UserInfo;
import com.spzx.user.service.IUserInfoService;
import org.springframework.util.DigestUtils;

/**
 * 会员Service业务层处理
 *
 * @author atguigu
 * @date 2024-07-03
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements IUserInfoService
{
//    @Autowired
//    private UserInfoMapper userInfoMapper;

    /**
     * 查询会员列表
     *
     * @param userInfo 会员
     * @return 会员
     */
    @Override
    public List<UserInfo> selectUserInfoList(UserInfo userInfo)
    {
        return baseMapper.selectUserInfoList(userInfo);
    }

    @Override
    public Boolean register(UserInfo userInfo) {
        String username = userInfo.getUsername();
        String phone = userInfo.getPhone();
        String password = userInfo.getPassword();
//        String lastLoginIp = userInfo.getLastLoginIp();
//        String nickName = userInfo.getNickName();

        long count = this.count(Wrappers.lambdaQuery(UserInfo.class)
                .eq(UserInfo::getUsername, username)
                .or()
                .eq(UserInfo::getPhone, phone));
        if(count>0){
            throw new ServiceException("手机号已被注册");
        }
        //密码加密：
        String encodPwd = DigestUtils.md5DigestAsHex(password.getBytes());
        //保存数据到数据库
        userInfo.setPassword(encodPwd);
        userInfo.setAvatar("http://192.168.123.168:8848/nacos/img/logo-2000-390.svg");
        userInfo.setPhone(username);
        userInfo.setStatus(1L);


        return baseMapper.insert(userInfo)>0;
    }

    @Override
    public UserInfo login(String username, String password) {
        //1、根据账号查询用户信息
        UserInfo userInfo = this.getOne(Wrappers.lambdaQuery(UserInfo.class)
                .eq(UserInfo::getUsername, username)
                .last("limit 1"));
        if(userInfo==null){
            throw new ServiceException("账号不存在");
        }
        //2、校验密码
        String encodPwd = DigestUtils.md5DigestAsHex(password.getBytes());
        if(!StringUtils.equals(encodPwd , userInfo.getPassword())){
            throw new ServiceException("密码错误");
        }
        //3、校验用户状态
        if(userInfo.getStatus()==null || userInfo.getStatus()!=1 ){
            throw new ServiceException("账号禁用");
        }
        return userInfo;
    }
    @Cacheable(value = "spzx:user:info" , key = "#userId")
    @Override
    public Map<String, String> getLoginUserInfo(Long userId) {
        //获取用户信息返回：
        /*
        {
        "nickName": "晴天",
        "avatar": "http://139.198.127.41:9000/sph/20230505/default_handsome.jpg"
    }
         */
        //1、获取用户id

        //2、查询用户信息
        UserInfo userInfo = this.getById(userId);
        Map<String, String> map = new HashMap<>();
        map.put("nickName",userInfo.getNickName());
        map.put("avatar" , userInfo.getAvatar());
        return map;
    }

}
